<template>
  <div>
    <video src="@/assets/video/222.mp4" loop autoplay muted></video>
    <div class="container">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
      <h1>管理系统</h1>
        <Common
          v-if="loginType === 'common'"
          ref="commonLogin"
          @syncLoginForm="handler"
          @submitForm="submitForm"
          @typeQr="loginType = 'Qr'"
          @typeSms="loginType = ''"
        ></Common>
        <Qr
          v-else-if="loginType === 'Qr'"
          :storageUserInfo="storageUserInfo"
          @typeCommon="loginType = 'common'"
        ></Qr>
        <Sms
          v-else
          @syncLoginForm="handler"
          @submitForm="submitForm"
          @typeCommon="loginType = 'common'"
          :storageUserInfo="storageUserInfo"
        ></Sms>
      </el-form>
    </div>
  </div>
</template>
<script>
import Common from "@/components/Login/commonLogin.vue";
import Qr from "@/components/Login/qrLogin.vue";
import Sms from "@/components/Login/smsLogin.vue";
import * as api from "@/api/users";
import storage from "@/utils/storage";

export default {
  data() {
    var validataCaptcha = (rule, value, callback) => {
      var userNamePattern = /^[a-zA-Z0-9_-]{3,16}$/;
      if (userNamePattern.test(value)) {
        callback();
      } else {
        callback("3-16位字母数字下划线");
      }
    };
    var validataUsername = (rule, value, callback) => {
      //用户名正则，3到16位（字母，数字，下划线，减号）
      var userNamePattern = /^[a-zA-Z0-9_-]{3,16}$/;
      if (userNamePattern.test(value)) {
        callback();
      } else {
        callback("3-16位字母数字下划线");
      }
    };
    var validataPassword = (rule, value, callback) => {
      var userNameCaptcha = /^[a-zA-Z0-9_-]{3,16}$/;
      if (userNameCaptcha.test(value)) {
        callback();
      } else {
        callback("3-16位字母数字下划线");
      }
    };
    return {
      loginType: "common",
      ruleForm: {
        username: "",
        password: "",
        captcha: "",
      },
      rules: {
        username: [{ validator: validataUsername, trigger: "blur" }],
        password: [{ validator: validataPassword, trigger: "blur" }],
        captcha: [{ validator: validataCaptcha, trigger: "blur" }],
      },
    };
  },
  methods: {
    handler(newVal) {
      this.ruleForm = newVal;
    },
    typeCommon() {
      this.loginType = "common";
    },
    submitForm() {
      // console.log(this.$refs)
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          // alert("submit!");
          api.verifyCaptcha(this.ruleForm.captcha).then(async (res) => {
            if (res.data.state) {
              //验证码通过
              console.log("验证码正确");
              let { username, password } = this.ruleForm;
              // console.log(username, password);
              let res = await api.loginApi(username, password);
              //登录成功
              if (res.data.state) {
                this.storageUserInfo(res.data);
                this.$router.push("/");
              } 
            } else {
              //验证码不通过
              this.$refs["commonLogin"].getCaptcha();
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //存储用户的信息和token
    storageUserInfo({ token, userInfo, permission }) {
      storage.set("userInfo", userInfo);
      storage.set("token", token);
      storage.set("permission", permission);
    },
  },

  components: { Common, Qr, Sms },
};
</script>

<style lang="less" scoped>
video {
  // 视频够宽，就不用设置此宽了
  width: 1540px;
  position: fixed;
  top: 0;
  left: 0;
}
.container {
  width: 500px;
  height: 400px;
  position: absolute;
  top: 50%;
  right: 4%;
  transform: translateY(-50%);
  border-radius: 6px;
  background-color: rgba(0, 0, 0, 0.3);
  // background-color: #fff;
}
h1 {
  text-align: center;
  color: #fff;
  // background-color: green;
}
</style>